package com.ksvip.next.components.banner;


import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import com.ksvip.next.components.banner.layout.BannerConstant;
import com.ksvip.next.components.banner.layout.Description;
import com.ksvip.next.components.banner.layout.DescriptionBanner;
import com.ksvip.next.components.banner.layout.LogoBanner;
import com.ksvip.next.components.core.constant.CommonConstants;
import com.taobao.text.Color;

public class BannerApplicationContextInitializer
        implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    private static final String LOGGER_CLASSPATH_LOCATION = "META-INF/maven/com.ksvip/next-components-starter-banner/pom.properties";

    private final ResourceLoader resourceLoader = new DefaultResourceLoader();

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        if (!(applicationContext instanceof AnnotationConfigApplicationContext)) {
            String nextVersion = CommonConstants.NEXT_VERSION_DEFAULT_VALUE;
            try {
                Resource appResource = this.resourceLoader.getResource(LOGGER_CLASSPATH_LOCATION);
                if (appResource.exists()) {
                    Properties appProperties = new Properties();
                    appProperties.load(new InputStreamReader(appResource.getInputStream()));
                    nextVersion = appProperties.getProperty("version");
                }
            } catch (Exception e) {
            }

            boolean linksShow = true;
            String deployEnv = System.getProperty(CommonConstants.DEPLOY_ENV);
            if (StringUtils.isNotEmpty(deployEnv)) {
                linksShow = StringUtils.equals(CommonConstants.DEPLOY_ENV_LOCAL, deployEnv);
            }
            LogoBanner logoBanner = new LogoBanner(BannerApplicationContextInitializer.class, "/logo.txt",
                    "Welcome to " + CommonConstants.NEXT_NAME, 1, 6,
                    new Color[]{Color.green, Color.red, Color.cyan, Color.blue, Color.yellow}, true);

            this.show(logoBanner, nextVersion, linksShow);
            System.setProperty(BannerConstant.BANNER_SHOWN, "false");
        }
    }

    private void show(LogoBanner logoBanner, String nextVersion, boolean linksShow) {
        String bannerShown = System.getProperty(BannerConstant.BANNER_SHOWN, "true");
        if (!Boolean.valueOf(bannerShown)) {
            return;
        }

        System.out.println("");
        String bannerShownAnsiMode = System.getProperty(BannerConstant.BANNER_SHOWN_ANSI_MODE, "false");
        if (Boolean.valueOf(bannerShownAnsiMode)) {
            System.out.println(logoBanner.getBanner());
        } else {
            System.out.println(logoBanner.getPlainBanner());
        }

        List<Description> descriptions = new ArrayList<>();


        descriptions.add(new Description(CommonConstants.FRAMEWORK + ":",
                CommonConstants.NEXT_NAME + " " + CommonConstants.FRAMEWORK + " (" +
                        CommonConstants.SPRING_BOOT_FRAMEWORK + ":" + CommonConstants.SPRING_BOOT_FRAMEWORK_VERSION + ")",
                0, 1));
        descriptions.add(new Description(CommonConstants.VERSION + ":", nextVersion, 0, 1));

        //only show in   company
        if (linksShow) {
            descriptions.add(new Description(CommonConstants.GITLAB + ":", CommonConstants.GITLAB_VALUE, 0, 1));
            descriptions.add(new Description(CommonConstants.DOCS + ":", CommonConstants.DOCS_VALUE, 0, 1));
        }

        DescriptionBanner descriptionBanner = new DescriptionBanner();
        System.out.println(descriptionBanner.getBanner(descriptions));
    }
}
